package cn.chencq.moudle.sys.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.chencq.constants.Constants;
import cn.chencq.moudle.sys.entity.SysRole;
import cn.chencq.moudle.sys.service.SysRoleMenuService;
import cn.chencq.moudle.sys.service.SysRoleService;
import cn.chencq.page.PageBean;
import cn.chencq.page.PageParam;
import cn.chencq.utils.R;
import cn.chencq.validator.ValidatorUtils;

/**
 * 角色管理
 * 
 * @author chenchaoqun
 * @email ccq@chencq.cn
 * @date 2018-10-22 15:41:40
 */
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	/**
	 * 角色列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:role:list")
	public R list(@ModelAttribute PageParam pageParam) {
		Map<String, Object> params = pageParam.getParams();
		// 如果不是超级管理员，则只查询自己创建的角色列表
		if (getUserId() != Constants.SUPER_ADMIN) {
			params.put("createUserId", getUserId());
		}
		PageBean page = sysRoleService.listPage(pageParam);
		return R.ok().put("page", page).put("params", params);
	}

	/**
	 * 角色列表
	 */
	@GetMapping("/select")
	@RequiresPermissions("sys:role:select")
	public R select() {
		Map<String, Object> params = new HashMap<>();
		// 如果不是超级管理员，则只查询自己所拥有的角色列表
		if (getUserId() != Constants.SUPER_ADMIN) {
			params.put("createUserId", getUserId());
		}
		List<SysRole> list = sysRoleService.queryList(params);
		return R.ok().put("list", list);
	}

	/**
	 * 角色信息
	 */
	@GetMapping("/info/{roleId}")
	@RequiresPermissions("sys:role:info")
	public R info(@PathVariable("roleId") Long roleId) {
		SysRole role = sysRoleService.selectByPrimaryKey(roleId);
		// 查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);
		return R.ok().put("role", role);
	}

	/**
	 * 保存角色
	 */
	@PostMapping("/save")
	@RequiresPermissions("sys:role:save")
	public R save(@RequestBody SysRole role) {
		ValidatorUtils.validateEntity(role);
		role.setCreateUserId(getUserId());
		sysRoleService.save(role);
		return R.ok();
	}

	/**
	 * 修改角色
	 */
	@PostMapping("/update")
	@RequiresPermissions("sys:role:update")
	public R update(@RequestBody SysRole role) {
		ValidatorUtils.validateEntity(role);

		role.setCreateUserId(getUserId());
		sysRoleService.updateRole(role);

		return R.ok();
	}

	/**
	 * 删除角色
	 */
	@PostMapping("/delete")
	@RequiresPermissions("sys:role:delete")
	public R delete(@RequestBody Long[] roleIds) {
		if (Objects.nonNull(roleIds) && roleIds.length > 0) {
			List<Long> roleIdList = Arrays.asList(roleIds);
			sysRoleService.batchDeleteByPks(roleIdList);
		}
		return R.ok();
	}
}
